/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import comu.Configuracio;
import controlador.autogenerats.ProduccioJpaController;
import entity.Camp;
import entity.Produccio;
import entity.Producte;
import java.util.ArrayList;
import java.util.Iterator;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author David
 */
public class ProduccioControlador extends ProduccioJpaController{

    public ProduccioControlador() {
        super(Persistence.createEntityManagerFactory(Configuracio.PERSISTANCE_UNIT));
    }

    
    public ArrayList<Produccio> findProduccioEntitiesByCamp(Camp camp, boolean acabada){
        EntityManager em = getEntityManager();
        try{
        Query q;
        if(acabada){
            q = em.createNativeQuery("select * from `produccio` where `camp_id` LIKE "+ camp.getId(), Produccio.class);
        }else{
            q = em.createNativeQuery("select * from `produccio` where ((`camp_id` LIKE "+ camp.getId() +" ) AND (`terminado` Like 0))", Produccio.class);
        }
        Iterator it = q.getResultList().iterator();
        ArrayList<Produccio> llista = new ArrayList<Produccio>();
        while(it.hasNext()){
            llista.add((Produccio) it.next());
        }
        return llista;
        } finally {
            em.close();
        }
    }

    public ArrayList<Produccio> findProduccioEntitiesByCampIProducte(Camp camp,Producte producte, boolean acabada) {
        EntityManager em = getEntityManager();
        try{
        Query q;
        if(acabada){
            q = em.createNativeQuery("select * from `produccio` where `camp_id` LIKE "+camp.getId()+" and `productes_id` like "+producte.getId(), Produccio.class);
        }else{
            q = em.createNativeQuery("select * from `produccio` where `camp_id` LIKE "+camp.getId()+
                    " and `productes_id` like "+producte.getId() +" AND `terminado` Like 0", Produccio.class);
        }
        Iterator it = q.getResultList().iterator();
        ArrayList<Produccio> llista = new ArrayList<Produccio>();
        while(it.hasNext()){
            llista.add((Produccio) it.next());
        }
        return llista;
        } finally {
            em.close();
        }
    }
    public Produccio findProduccionEntitiByLastCollitaForProducte(Producte producte) {
        EntityManager em = getEntityManager();
        try{
            Query q;
            q = em.createNativeQuery("select * from produccio where id = (select produccio_id from collita where produccio_id = any (select `id` from `produccio` where `productes_id` = '" + producte.getId() +"') order by `data` desc limit 1)", Produccio.class);
            if(q.getResultList().isEmpty()){
                return null;
            } else{
                return (Produccio)q.getResultList().get(0);
            }
        } finally {
            em.close();
        }
    }
    public Produccio findFirstProduccionEntitiByProducte(Producte producte) {
        EntityManager em = getEntityManager();
        try{
            Query q;
            q = em.createNativeQuery("select * from `produccio` where `productes_id` like "+ producte.getId() +" limit 1", Produccio.class);
            if(q.getResultList().isEmpty()){
                return null;
            } else{
                return (Produccio)q.getResultList().get(0);
            }
        } finally {
            em.close();
        }
    }
    
}
